//
//  HDSegmentMenu.h
//  Wujie
//
//  Created by haidong on 2017/4/24.
//  Copyright © 2017年 WuJieShengHuo. All rights reserved.
//

#import "HDSegmentMenu.h"

@interface HDSegmentMenu ()
{
    CGFloat buttonWidth;
    UIView *_slideLine;
    UIColor *_selectedColor;
    CGFloat _itemSpace;
    NSInteger _index;
}
@end

@implementation HDSegmentMenu

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.userInteractionEnabled = YES;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titlesArray selectedColor:(UIColor *)selectedColor itemSpace:(float)space withSelectedIndex:(NSInteger)index{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.userInteractionEnabled = YES;
        self.titlesArr = [titlesArray mutableCopy];
        _selectedColor = selectedColor;
        _itemSpace = space;
        _index = index;
        [self itemsInit];
        
    }
    return self;
}

- (void)itemsInit {
    for (UIView *subView in self.subviews) {
        [subView removeFromSuperview];
    }
//    buttonWidth = (self.sizeWidth - (((float)self.titlesArr.count) + 1) * _itemSpace)/(float)self.titlesArr.count;
    buttonWidth = (self.width - (((float)self.titlesArr.count) + 1) * _itemSpace)/(float)self.titlesArr.count;
    for (int i = 0; i < self.titlesArr.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.frame = CGRectMake((buttonWidth + _itemSpace) * i + _itemSpace, 0, buttonWidth, self.sizeHeight - SlideLineHeight);
        button.frame = CGRectMake((buttonWidth + _itemSpace) * i + _itemSpace, 0, buttonWidth, self.height - SlideLineHeight);
        [button setTitle:self.titlesArr[i] forState:UIControlStateNormal];
        
        button.titleLabel.font = FontSize(14);
        [button setTitleColor:TextAssistColor forState:UIControlStateNormal];
        [button setTitleColor:_selectedColor forState:UIControlStateSelected];
        
        button.tag = 10+i;
        if (i == _index) {
            button.selected = YES;
        }
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
    }
//    _slideLine = [[UIView alloc] initWithFrame:CGRectMake(_itemSpace, self.sizeHeight - SlideLineHeight, buttonWidth, SlideLineHeight)];
    
    _slideLine = [[UIView alloc] initWithFrame:CGRectMake(_itemSpace + (_index * (buttonWidth + _itemSpace)), self.height - SlideLineHeight, buttonWidth, SlideLineHeight)];
    _slideLine.backgroundColor = _selectedColor;
    [self addSubview:_slideLine];
}

- (void)buttonAction:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            ((UIButton *)subView).selected = NO;
        }
        sender.selected = YES;
    }
    NSInteger index = sender.tag - 10;
    CGFloat slideLineX = _itemSpace + (index * (buttonWidth + _itemSpace));
    [UIView animateWithDuration:0.2 animations:^{
        CGRect frame = _slideLine.frame;
        frame.origin.x = slideLineX;
        _slideLine.frame = frame;
    }];
    if (self.delegate && [self.delegate respondsToSelector:@selector(segmentMenuSelectAtIndex:)]) {
        [self.delegate segmentMenuSelectAtIndex:index];
    }
    if (self.segmentSelectedBlock) {
        self.segmentSelectedBlock(index);
    }
}

- (void)setButtonSelectedAtIndex:(NSInteger)index {
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            ((UIButton *)subView).selected = NO;
        }
    }
    UIButton *btn = [self viewWithTag:10 + index];
    if (btn) {
        btn.selected = YES;
        CGFloat slideLineX = _itemSpace + (index * (buttonWidth + _itemSpace));
        [UIView animateWithDuration:0.2 animations:^{
            CGRect frame = _slideLine.frame;
            frame.origin.x = slideLineX;
            _slideLine.frame = frame;
        }];
    } else {
        NSLog(@"越界");
    }
}

- (NSArray *)titlesArr {
    if (!_titlesArr) {
        _titlesArr = [NSArray array];
    }
    return _titlesArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
